<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2020/1/3
 * Time: 9:51
 */
namespace App\Repositories\System;
use App\Common\Utils\BaseRepository;
use App\Models\System\Scale;

/**
 * 系统管理-量表 (仓库)
 */
class ScaleRepository extends BaseRepository
{
    public $model;
    public function __construct(Scale $scale)
    {
        $this->model = $scale;
    }
    
    public function  toTableData($detail, $assessScore = NULL)
    {
        $result = [];
        
        if($detail->count()) {
            foreach($detail as $d) {
                $tableData['id'] = $d->id;
                $tableData['pid'] = $d->pid;
                $tableData['top_id'] = $d->top_id;
                $tableData['scale_id'] = $d->scale_id;
                $tableData['score'] = $d->score;
                $tableData['desc'] = $d->desc;
                $tableData['self_rate'] = $d->self_rate;
                $tableData['reviewer'] = $d->reviewer;
                $tableData['review_name'] = $d->review_name;
                $tableData['level'] = $d->level;
                $tableData['tier'] = $d->tier;
                $tableData['min_score'] = $d->min_score;
                $tableData['max_score'] = $d->max_score;
                $tableData['module'] = $d->module;
                $tableData['module_text'] = $d->module_text;
                $tableData['scale_index'] = $d->scale_index;
                $tableData['single_score'] = $d->single_score;
                $tableData['default_score'] = $d->default_score;
                $tableData['check_module'] = $d->check_module;
                $tableData['check_module_text'] = $d->check_module_text;


                if($assessScore) {
                    $scoreData = $assessScore->where('detail_id', $d->id)->first();
                    $selfScore = $scoreData ? $scoreData->self_score : 0;
                    $rateScore = $scoreData ? $scoreData->rate_score : 0;
                    $tableData['self_score'] = $selfScore > 0 ? $selfScore : '';
                    $tableData['rate_score'] = $rateScore > 0 ? $rateScore : '';
                }

                $title = $this->toDealData($d);

                foreach($title as $t => $v) {
                    $tableData[$t] = $v;
                }
                $result[] = $tableData;
            }
        } else {
            return [];
        }
        
        return $result;
    }

    //传数组
    public function toTableData2($detail, $assessScore = NULL)
    {
        $result = [];
        if(!empty($detail)) {
            foreach($detail as $d) {
                $tableData['id'] = $d['id'];
                $tableData['pid'] = $d['pid'];
                $tableData['top_id'] = $d['top_id'];
                $tableData['scale_id'] = $d['scale_id'];
                $tableData['score'] = $d['score'];
                $tableData['desc'] = $d['desc'];
                //$tableData['self_rate'] = $d->self_rate;
                //$tableData['reviewer'] = $d->reviewer;
                //$tableData['review_name'] = $d->review_name;
                $tableData['level'] = $d['level'];
                $tableData['tier'] = $d['tier'];
                $tableData['single_score'] = $d['single_score'];
                $tableData['default_score'] = $d['default_score'];
                $tableData['check_module'] = $d['check_module'];
                $tableData['check_module_text'] = $d['check_module_text'];

                // if($assessScore) {
                //     $scoreData = $assessScore->where('detail_id', $d['id'])->first();
                //     $selfScore = $scoreData ? $scoreData->self_score : 0;
                //     $rateScore = $scoreData ? $scoreData->rate_score : 0;
                //     $tableData['self_score'] = $selfScore > 0 ? $selfScore : '';
                //     $tableData['rate_score'] = $rateScore > 0 ? $rateScore : '';
                // }

                $title = $this->toDealData2($d);

                foreach($title as $t => $v) {
                    $tableData[$t] = $v;
                }
                $result[] = $tableData;
            }
        } else {
            return [];
        }
        
        return $result;
    }

    /**
     * @Description: 递归处理数据
     * @Author: kzj
     * @datetime: 2022-11-26
     */
    public function toDealData($d, $result = [], $span = [])
    {
        $result['title_arr'][] = ['id' => $d->id, 'name' => $d->title, 'level' => $d->level];
        $result['title'.$d->level] = $d->title;

        if($d->child_count > 1) {
            $span[] = ['id' => $d->id, 'col' => $d->level];
        }

        if($d->parent) {
            return $this->toDealData($d->parent, $result, $span);
        }

        $result['span'] = $span;

        return $result;
    }

     //传数组
     public function toDealData2($d, $result = [], $span = [])
     {
         $result['title_arr'][] = ['id' => $d['id'], 'name' => $d['title'], 'level' => $d['level']];
         $result['title'.$d['level']] = $d['title'];
 
         // if(!empty($d['child_count']) && $d['child_count'] > 1) {
         //     $span[] = ['id' => $d['id'], 'col' => $d['level']];
         // }
 
         if($d['parent']) {
             return $this->toDealData2($d['parent'], $result, $span);
         }
 
         $result['span'] = $span;
 
         return $result;
     }
}
